//  iDMSCalendar JavaScript file
/**

iDMS Calendar is a javascript object to display a "popup" calendar.  The calendar created is a floating <div> layer 
and is completely virtual. The calendar can be setup to different sizes and modify a CSS file for looks.

Implementation looks like this:

<script language="javascript" type="text/javascript">
var d = "2005-10-16"; // format for date is YYYY-MM-DD
sdate = new iDMSCalendar(d,200,200); // construct with d(date), 200(width),200(height)
SetPreURL(""); // sets the location of the idms/img folder, so if outside of idms base project, put "idms/"
sdate.setName("sdate"); // sets the name inside of the calendar object for internal Javascript calls
sdate.MakeMonth(d,sdate); // displays the calendar to the given date (usually same as constructed)
</script>
<input type="hidden" name="sdate_val" value="" id="sdate_val" /> <!-- Not needed, but easy to find sdate_val in $_POST array! -->

Easy way to get the date clicked on (in Javascript):

var scd_val = document.getElementById("iDMSCalendar_" + sdate.getCalName() + "_date").value;

If the <input type="hidden"> tag was used, then to fill that hidden field with the date:
document.getElementById("sdate_val").value = scd_val;

*/


var PRE_URL = "idms/"; // location of idms base (DO NOT CHANGE HERE, use SetPreURL() in your code)
/**
* Takes date from YYYY-MM-DD (MySQL Format) and converts to MM/DD/YYYY format
*/
function GetDateFromDB(d)
{
    var m = d.substr(5,2);
    var day = d.substr(8,2);
    var y = d.substr(0,4);
    return (m + "/" + day + "/" + y);
}
/**
* Sets URL difference if outside of standard idms/ URL
* STATIC FUNCTION
*/
function SetPreURL(pre)
{
    PRE_URL = pre;	
}
/**
* Outputs todays date in format YYYY-MM-DD (MySQL Format)
*/
function getTodayDate()
{
    var d = new Date();
    var today = d.getFullYear() + "-";
    if((d.getMonth()+1)<10)
        today += "0";
    today += (d.getMonth()+1) + "-";
    if(d.getDate()<10)
        today += "0";
    today += d.getDate();
    return today;
}

var monthsArray = new Array(20);
monthsArray[0]  = "Jan";
monthsArray[1]  = "Feb";
monthsArray[2]  = "Mar";
monthsArray[3]  = "Apr";
monthsArray[4]  = "May";
monthsArray[5]  = "Jun";
monthsArray[6]  = "Jul";
monthsArray[7]  = "Aug";
monthsArray[8]  = "Sep";
monthsArray[9]  = "Oct";
monthsArray[10] = "Nov";
monthsArray[11] = "Dec";

var dayStr = new Array(7);
dayStr[0] = "S";
dayStr[1] = "M";
dayStr[2] = "T";
dayStr[3] = "W";
dayStr[4] = "T";
dayStr[5] = "F";
dayStr[6] = "S";

function getSeed() {
    var today = new Date();
    var seed = (today.getTime()*84363) % 8630;
    return (seed/(934320))*1000;
}

function rand(number) {
    return Math.floor(getSeed()*203183);
}
/**
* @desc:  iDMS Calendar Constructor
* Have to set all functions and variables
*/
function iDMSCalendar(indate,width,height,t,l) {
    // Callable Functions
    this.MakeMonth = MakeMonth;
    this.ShowDate = ShowDate;
    this.SetToDate = SetToDate;
    this.getSelectedDate = getSelectedDate;
    this.getCalName = getCalName;
    this.Setup = Setup;
    this.getMonthStr = getMonthStr;
    this.getNextMonth = getNextMonth;
    this.getPrevMonth = getPrevMonth;
    this.getNextYear = getNextYear;
    this.getPrevYear = getPrevYear;
    this.setName = setName;
    this.setDate = setDate;
    this.display = display;
    this.callback = callback;
    
    this.defaultDate = indate;
    this.SetToDate(indate);
    this.calName = "iDMSCalendar" + rand(); // 4 digits
    this.width = width;
    this.height = height; // add 30 for title bar at creation of division
    SetClientVariables();
    
    this.topLayer = 0;
    this.leftLayer = 0;
    
    if(t>=0)
        this.topLayer = t;
    else
        this.topLayer = (CLIENT_HEIGHT-200)/2;
    if(l>=0)
        this.leftLayer = l;
    else
        this.leftLayer = (CLIENT_WIDTH-200)/2;
    this.Setup();
    this.selectedDate = "0";
    /**
    * This is the copy operator (for OBJ(A) = OBJ(B))
    */
    function copyOperator(obj) {
        this.y = obj.y; // year
        this.m = obj.m; // month
        this.d = obj.d; // day
        this.topLayer = obj.topLayer;
        this.leftLayer = obj.leftLayer;
        
        this.selectedDate = obj.selectedDate;
        this.defaultDate = obj.defaultDate;
        
        this.MakeMonth = obj.MakeMonth;
        this.display = obj.display;
        this.callback = obj.callback;
        this.ShowDate = obj.ShowDate;
        this.getMonthStr = obj.getMonthStr;
        this.getNextMonth = obj.getNextMonth;
        this.getPrevMonth = obj.getPrevMonth;
        this.getNextYear = obj.getNextYear;
        this.getPrevYear = obj.getPrevYear;
        this.getCalName = obj.getCalName;
        this.calName = obj.calName;
        this.varName = obj.varName;
        this.setName = setName;
        this.setDate = setDate;
        this.getSelectedDate = getSelectedDate;
    }
    
    /**
    * Idea I had, haven't implemented it...idea is when the calendar is clicked, you can add some code to run too!
    */
    function callback(){
        
    }
    /**
    * Gets name of calendar
    */
    function getCalName() {
        return this.calName;
    }
    // assuming date is YYYY-MM-DD format ONLY
    function SetToDate(indate) {
        // must do parseInt(no,10); because of 8 and 9 error in JavaScript
        // http://www.breakingpar.com/bkp/home.nsf/Doc?OpenNavigator&U=87256B280015193F87256C85006A6604 <--shows error
        this.y = parseInt(indate.substr(0,4));
        this.m = parseInt(indate.substr(5,2),10);
        this.d = parseInt(indate.substr(8,2),10);
    }
    
    /**
    * @desc: After day has been clicked, sets date in hidden field
    */
    function setDate(day) {
        var sM = this.m;
        var sD = parseInt(day,10);
        if(this.m < 10)
            sM = "0" + this.m;
        if(sD<10)
            sD = "0" + sD;
        this.selectedDate = this.y + "-" + sM + "-" + sD;
        document.getElementById("iDMSCalendar_" + this.calName + "_date").value = this.selectedDate;
        this.MakeMonth(this.selectedDate,this);
        this.callback();
        return this.display();
    }
    
    function getSelectedDate() {
        return this.selectedDate;
    }
    
    function getNextYear() {
        var nD;
        var nM;
        var nY;
        nD = this.d;
        nM = this.m;
        nY = this.y + 1;
        
        if(nM < 10)
            nM = "0" + nM;
        if(nD < 10)
            nD = "0" + nD;
        
        nM = nM + ""; // convert to string	
        nY = nY + "";
        nD = nD + "";
        return nY + "-" + nM + "-" + nD;
    }
    
    function getPrevYear()
    {
        var nD;
        var nM;
        var nY;
        nD = this.d;
        nM = this.m;
        nY = this.y-1;
        
        if(nM < 10)
            nM = "0" + nM;
        if(nD < 10)
            nD = "0" + nD;
        nM = nM + ""; // convert to string	
        nY = nY + "";
        nD = nD + "";
        return nY + "-" + nM + "-" + nD;
    }
    
    function getNextMonth()
    {
        var nM;
        var nY;
        var nD;
        nD = this.d;
        nM = this.m;
        nY = this.y;
        if(nM==12)
            {
                nY = nY + 1;
                nM = 1;
            }
            else
                {
                    nM = nM + 1;
                }
                
                if(nM < 10)
                    nM = "0" + nM;
                if(nD < 10)
                    nD = "0" + nD;
                nM = nM + ""; // convert to string		
                nY = nY + "";
                return nY + "-" + nM + "-" + nD;
            }
            
            function getPrevMonth()
            {
                var pM;
                var pY;
                var pD;
                pD = this.d;
                pM = this.m;
                pY = this.y;
                if(pM==1)
                    {
                        pY = pY - 1;
                        pM = 12;
                    }
                    else
                        {
                            pM = pM - 1;
                        }
                        if(pD < 10)
                            pD = "0" + pD;
                        if(pM < 10) // formating only
                            pM = "0" + pM;
                        return pY + "-" + pM + "-" + pD;
                    }
                    
                    function display()
                    {
                        var x = document.getElementById("iDMSCalendar_" + this.calName);
                        var tmp = (x.style.visibility=="hidden")?"visible":"hidden";
                        document.getElementById("iDMSCalendar_" + this.calName).style.visibility = tmp;
                        return false; // don't let the link bubble through
                    }
                    
                    function Setup()
                    {
                        var style = "style=\"visibility:hidden;position:absolute;width:" + this.width + "px;height:" + (this.height+23) + "px;\"";
                        //used for linking calendar image and anchor link
                        document.write("<div id=\"iDMSCalendar_a_" + this.calName + "\">&nbsp;</div>");
                        document.write("<div id=\"iDMSCalendar_" + this.calName + "\" class=\"iDMSCalendarWin\" " + style + ">");
                        document.write("</div>");
                    }
                    
                    /* Must send in obj for copy operator...*/
                    function MakeMonth(indate,obj)
                    {
                        copyOperator(obj);
                        var cal = document.getElementById("iDMSCalendar_" + this.calName);
                        cal.style.top = this.topLayer + "px";
                        cal.style.left = this.leftLayer + "px";
                        cal.style.zIndex = 3000;
                        this.SetToDate(indate);
                        var name = "iDMSCalendar_" + this.calName;
                        document.getElementById(name).innerHTML = this.ShowDate(indate.substr(8,2));
                        document.getElementById(this.calName + "_monthHeader").innerHTML = "<span class=\"iDMSCalendarMonth\">" + this.getMonthStr() + " - " + this.y + "</span>";
                        //position center
                        return false;
                    }
                    
                    /**
                    * Creates input and icon to open/close and view date
                    */
                    function setName(name) {
                        this.varName = name;
                        var anchor = document.getElementById("iDMSCalendar_a_" + this.calName);
                        var line = "<table width=\"100\" border=\"0\"><tr>" +
                        "<td><a href=\"\" onclick=\"return " + this.varName + ".display();\"><img src=\"" + PRE_URL + "icons/calendaricon.gif\" id=\"iDMSCalendar_" + this.calName + "_img\" align=\"center\" border=\"0\" alt=\"Calendar Icon\" /></a></td><td><input type=\"text\" size=\"10\"";
                        //if(!document.all)  // disable if not IE
                        line += " disabled disabled=\"yes\" ";
                        line +=  "maxlength=\"10\" value=\"" + this.defaultDate + "\" name=\"iDMSCalendar_" + this.calName + "_date\" id=\"iDMSCalendar_" + this.calName + "_date\"></td>";
                        line += "</tr></table>";
                        anchor.innerHTML = line;
                    }
                    
                    /**
                    * Draws out calendar 
                    * // highlight day
                    */
                    function ShowDate(h_day) {
                        var tableBegin = "<td height=\"" + (this.height/9) + "\"  align=\"center\">";
                        var tableEnd = "</td>";
                        var offset = getFirstDayOfMonth(this.m,this.y);
                        var lastDay = getLastDayOfMonth(this.m,this.y);
                        var dayInWeek = 0;
                        var weekCounter = false;

                        var retVal = "";
                        retVal += "<table width=\"" + this.width + "\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\">";
                        retVal += "<tr><td colspan=\"7\" id=\"iDMSCalendarTitleBar\" align=\"right\" style=\"padding-right:10px;padding-top:3px;padding-bottom:3px;\"><a href=\"#\" onclick=\"return " + this.varName + ".display();\"><img src=\"" + PRE_URL + "icons/no.gif\" border=\"0\" alt=\"Close\" title=\"Close\" /></a></td></tr>";
                        retVal += "<tr>";
                        retVal += "<td colspan=\"1\ align=\"center\" width=\"21\"><a href=\"#\" onclick=\"return " + this.varName + ".MakeMonth('"+ this.getPrevYear() + "'," + this.varName + ");\"><img src=\"" + PRE_URL + "icons/larrow2.gif\" align=\"center\" alt=\"Previous Year\" title=\"Goto Previous Year\" border=\"0\" /></a></td>";
                        retVal += "<td colspan=\"1\ align=\"center\" width=\"20\"><a href=\"#\" onclick=\"return " + this.varName + ".MakeMonth('"+ this.getPrevMonth() + "'," + this.varName + ");\"><img src=\"" + PRE_URL + "icons/larrow1.gif\" align=\"center\" alt=\"Previous Month\" title=\"Goto Previous Month\" border=\"0\" /></a></td>";
                        retVal += "<td colspan=\"3\" align=\"center\"><div id=\"" + this.calName + "_monthHeader\"></div></td>";
                        retVal += "<td colspan=\"1\" align=\"center\" width=\"20\"><a href=\"#\" onclick=\"return " + this.varName + ".MakeMonth('"+ this.getNextMonth() + "'," + this.varName + ");\"><img src=\"" + PRE_URL + "icons/rarrow1.gif\" align=\"center\" alt=\"Next Month\" title=\"Goto Next Month\" border=\"0\" /></a></td>";
                        retVal += "<td colspan=\"1\" align=\"center\" width=\"21\"><a href=\"#\" onclick=\"return " + this.varName + ".MakeMonth('"+ this.getNextYear() + "'," + this.varName + ");\"><img src=\"" + PRE_URL + "icons/rarrow2.gif\" align=\"center\" alt=\"Next Year\" title=\"Goto Next Year\" border=\"0\" /></a></td>";
                        retVal += "</tr><tr>";
                        // Days of week (S M T W T F S)
                        for(var d=0;d<7;d++)
                            retVal += "<td height=\"" + (this.height/9) + "\"  width=\"" + (this.width/7) + "\ align=\"center\"><div style=\"width:" + (this.width/8) + "px;text-align:center;\"><span class=\"weektitle\">" + getDayStr(d) + "</span></div></td>";
                        retVal += "</tr><tr>";
                        // Make first of week start at first of week
                        for(var q=0;q<offset;q++)
                            retVal += "<td id=\"iDMSCalendarNoDay\">&nbsp;</td>";
                        // Start output of days
                        for(var i=1;i<=lastDay;i++)
                            {
                                if((i+offset)%7==1)
                                    {
                                        retVal += "<tr>";
                                        dayInWeek = 0;
                                        weekCounter = !weekCounter;
                                    }
                                    // Selected Date highlights here with iDMSCalendarSelect
                                    retVal += tableBegin;
                                    if(i==parseInt(h_day))
                                        retVal += "<div style=\"cursor:pointer;\" id=\"iDMSCalendarSelect\" onclick=\"return " + this.varName + ".setDate(" + i + ");\">";
                                    else if(weekCounter)
                                        retVal += "<div style=\"cursor:pointer;\" id=\"iDMSCalendarTD\" onclick=\"return " + this.varName + ".setDate(" + i + ");\">";
                                    else
                                        retVal += "<div style=\"cursor:pointer;\" id=\"iDMSCalendarTD1\" onclick=\"return " + this.varName + ".setDate(" + i + ");\">";
                                    retVal += "<span class=\"daytext\">" + i + "</span></div>" + tableEnd;
                                    dayInWeek++;
                                    if(((i+offset)%7==0)&&(i+offset+1 < lastDay))
                                        {
                                            retVal += "</tr>";
                                        }
                                    }
                                    for(d=0;d<(7-dayInWeek);d++)
                                        retVal += "<td id=\"iDMSCalendarNoDay\">&nbsp;</td>";
                                    
                                    retVal += "</tr></table>";
                                    return retVal;
                                }	
                                
                                function getDayStr(inDay)
                                {
                                    return dayStr[inDay];
                                }
                                
                                function getFirstDayOfMonth(mon,y)
                                {
                                    var firstOfMonth = new Date(mon + "/01/" + y);
                                    return firstOfMonth.getDay();
                                }
                                
                                function getLastDayOfMonth(mon,y)
                                {
                                    var retVal = 0;
                                    switch(parseInt(mon))
                                    {
                                        case 1:
                                        case 3:
                                        case 5:
                                        case 7:
                                        case 8:
                                        case 10:
                                        case 12:
                                        retVal = 31;
                                        break;
                                        case 2: // get a leap year...2000, 2004, 2008 etc
                                        if((y%4)==0)
                                            retVal = 29;
                                        else
                                            retVal = 28;
                                        break;
                                        case 2:
                                        case 4:
                                        case 6:
                                        case 9:
                                        case 11:			
                                        retVal = 30;
                                        break;
                                    }
                                    return retVal;		
                                }
                                function getMonthStr()
                                {
                                    return monthsArray[this.m-1];
                                }
                                
                            }